package com.sharp.authorization.annotation.auth;

import com.sharp.authorization.bo.account.AccountInfo;
import com.sharp.authorization.bo.permit.PermitInfo;
import com.sharp.authorization.bo.role.RoleInfo;
import com.sharp.authorization.bo.user.UserInfo;
import com.sharp.authorization.constants.common.RequestConst;
import com.sharp.authorization.constants.token.TokenItemConst;
import com.sharp.authorization.holder.AccountContextHolder;
import com.sharp.authorization.holder.TokenHolder;
import com.sharp.authorization.service.token.TokenService;
import com.sharp.authorization.utils.token.AuthTokenUtil;
import com.sharp.kernel.constant.err.ErrCodeSys;
import com.sharp.kernel.utils.JsonUtil;
import com.sharp.kernel.validate.Validator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Title: LoginAuthRole
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2021/8/23 14:17
 */
@Slf4j
@Component
public class LoginAuthRole implements AuthRole {
    @Resource
    private TokenService tokenService;

    @Override
    public void validate(HttpServletRequest request) {
        String token = AuthTokenUtil.extractToken(request);
        Validator.assertNotBlank(token, ErrCodeSys.PA_DATA_NOT_EXIST, "安全令牌");
        String operator = request.getHeader(RequestConst.USER_ID);
        Validator.assertNotBlank(token, ErrCodeSys.PA_DATA_NOT_EXIST, "用户名");
        tokenService.verifyToken(token, operator);
        buildContext(token);
    }

    public void buildContext(String token) {
        TokenHolder.set(token);
        Map<String, Object> map = tokenService.getTokenValue(token);
        UserInfo userInfo = buildUserInfo(map);
        Map<String, RoleInfo> roleInfoMap = JsonUtil.readJson(map.get(TokenItemConst.ROLE_MAP.name()), Map.class, String.class, RoleInfo.class);
        List<PermitInfo> permitInfoList = JsonUtil.readJson(map.get(TokenItemConst.PERMIT_LIST.name()), List.class, PermitInfo.class);
        userInfo.setRoleInfoMap(roleInfoMap);
        userInfo.setPermitInfoList(permitInfoList);
        AccountInfo accountInfo = buildAccountInfo(map);
        accountInfo.setUserInfo(userInfo);
        AccountContextHolder.set(accountInfo);
    }

    private UserInfo buildUserInfo(Map<String, Object> map) {
        UserInfo userInfo = new UserInfo();
        if (map.get(TokenItemConst.NICK_NAME.name()) != null) {
            userInfo.setName(String.valueOf(map.get(TokenItemConst.NICK_NAME.name())));
        }
        if (map.get(TokenItemConst.OPERATOR.name()) != null) {
            userInfo.setUserId(String.valueOf(map.get(TokenItemConst.OPERATOR.name())));
        }
        if (map.get(TokenItemConst.PHONE_NUMBER.name()) != null) {
            userInfo.setPhoneNumber(String.valueOf(map.get(TokenItemConst.PHONE_NUMBER.name())));
        }
        if (map.get(TokenItemConst.EMAIL.name()) != null) {
            userInfo.setEmail(String.valueOf(map.get(TokenItemConst.EMAIL.name())));
        }
        return userInfo;
    }

    private AccountInfo buildAccountInfo(Map<String, Object> map) {
        AccountInfo accountInfo = new AccountInfo();
        if (map.get(TokenItemConst.TOKEN.name()) != null) {
            accountInfo.setToken(String.valueOf(map.get(TokenItemConst.TOKEN.name())));
        }
        if (map.get(TokenItemConst.CLIENT_TYPE.name()) != null) {
            accountInfo.setClientType(String.valueOf(map.get(TokenItemConst.CLIENT_TYPE.name())));
        }
        if (map.get(TokenItemConst.OPERATOR.name()) != null) {
            accountInfo.setOperator(String.valueOf(map.get(TokenItemConst.OPERATOR.name())));
        }
        return accountInfo;
    }


}
